package io;

import java.io.*;

public class ObjectInputOutputStreamTest {
	
	
	
	
	public static void main(String[] args) {
		
		Person p1 = new Person(18,"С��");
		Person p2 = new Person(17,"С��");
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		
		try {
				
			/*���л����̣����ڴ��е�ObjectOutputStreamת��Ϊ�����������洢��Ӳ����*/		
			oos = new ObjectOutputStream(new FileOutputStream("src\\io\\Person.txt"));
			oos.writeObject(p1);
			oos.flush();
			oos.writeObject(p2);
			oos.flush();
			
			/*�����л����̣���Ӳ���е��ļ�ͨ��ObjectInputStreamת��Ϊ��Ӧ�Ķ���*/
			ois = new ObjectInputStream(new FileInputStream("src\\io\\Person.txt"));
			Person p3 = (Person)ois.readObject(); 
			System.out.println(p3.getAge()+p3.getName());
			Person p4 = (Person)ois.readObject(); 
			System.out.println(p4.getAge()+p4.getName());
			
		}catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(oos!=null){
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		
	}
}

/*		ʵ�����л����Ҫ��
 *		1.ʵ��Serializable�ӿ�
 *		2.�������ͬ��Ҫʵ��Serializable�ӿ�(String,Integer) 
 *		3.���鶨��serialVersionUID
 *		4.static��transient���εı�������ʵ�����л�
 * */
class Person implements Serializable{
	
	int age;
	String name;
	private static final long serialVersionUID = 6513264163L;//���鶨��
	
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Person(int age, String name){
		this.age = age;
		this.name = name;
	}
}